const express = require('express')
const route = express.Router()
const advModel = require('../db/model/advModel')
const fs = require('fs')
const path = require('path')
//引入权限中间件
const isLoginMiddle = require("../middleware/isLoginMiddle")

route.delete('/deleteAdv', isLoginMiddle, async (req, res) => {
    const {
        _id
    }=req.query
    //删除添加的upload图片

    //找到对应的_id名字
    try{
        const{
            advHref
        }=await advModel.findOne({
            _id
        })
      
        const imgName =  advHref.split('/')[2]


        fs.unlink(path.resolve(__dirname,'../upload',imgName),()=>{
            console.log('删除成功')
        })
    }catch(e){
         console.log("删除广告列表失败" + e.message);
        return  res.send({
        code: 10001,
        msg: "删除广告列表失败" + e.message,
        data: null
    })
    }




    //删除数据库
    try {
        await advModel.deleteOne({
            _id
        })
        res.send({
            code: 10000,
            msg: "删除广告列表成功",
            data: null
        })
    } catch (e) {
        console.log("删除广告列表失败" + e.message);
        res.send({
            code: 10001,
            msg: "删除广告列表失败" + e.message,
            data: null
        })
    }

})

module.exports = route

